package com.maimang.platform.crm.util;

import com.maimang.platform.common.entity.SysDeptTree;
import com.maimang.platform.crm.entity.WorkTreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname TreeHelper
 * @description <br>树形结构转换工具类</br>
 * @date 2019-10-11 14:53
 * @email jintaozhao@qq.com
 */
public class TreeHelper {

	/**
	 * 将list转换成树
	 *
	 * @param list 目标list
	 * @return
	 */
	public static List<SysDeptTree> buildTree(List<SysDeptTree> list, Long rootDeptId) {
		List<SysDeptTree> tree = new ArrayList<>();

		for (SysDeptTree node : list) {
			if (node.getParentId().equals(rootDeptId)) {
				List<SysDeptTree> deptTreeList = buildTree(list, node.getDeptId());
				node.setChildren(deptTreeList);
				tree.add(node);
			}
		}
		return tree;
	}


	/**
	 * 将list转换成树
	 *
	 * @param list 目标list
	 * @return
	 */
	public static List<WorkTreeNode> buildWorkTree(List<WorkTreeNode> list, Long rootDeptId) {
		List<WorkTreeNode> tree = new ArrayList<>();

		for (WorkTreeNode node : list) {
			if (node.getParentId().equals(rootDeptId)) {
				List<WorkTreeNode> deptTreeList = buildWorkTree(list, node.getDeptId());
				node.setChildren(deptTreeList) ;
				tree.add(node);
			}
		}
		return tree;
	}
}
